package com.example.service;

import cn.hutool.json.JSONObject;
import com.example.common.Result;
import com.example.entity.*;
import com.example.entity.Class;
import com.example.entity.others.Test_User_Other;
import com.example.entity.others.User_Question;
import com.example.exception.ServiceException;
import com.example.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Resource
    UserMapper userMapper;

    public User login(User user) {
        User dbUser = userMapper.selectByUsername(user.getAccount());
        System.out.println(dbUser);
        if(dbUser==null){
            throw new ServiceException("用户名或密码错误");
        }
        if(!dbUser.getPassword().equals(user.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }
        return dbUser;
    }

    public List<User> userList(User user) {
        List<User> userList = userMapper.getUserList(user);
        return userList;
    }

    public List<Test> testList(Test test) {
        List<Test> testList = userMapper.getTestList(test);
        return testList;
    }

    public Object questionAll(Test_User_Other testUserOther) {
        Map<String, List<Question>> map = new HashMap<>();

        List<Question> userQuestionC = userMapper.getUqcByTestid(testUserOther.getTest().getTid());
        List<Question> userQuestionF = userMapper.getUqfByTestid(testUserOther.getTest().getTid());
        List<Question> userQuestionS = userMapper.getUqsByTestid(testUserOther.getTest().getTid());

        map.put("选择题", userQuestionC);
        map.put("填空题", userQuestionF);
        map.put("解答题", userQuestionS);

        return map;
    }

    public Object testDelete(Test_User testUser) {
        userMapper.deleteTest(testUser);
        userMapper.deleteTestUser(testUser);
        List<Integer> qidList = userMapper.getQIdByTId(testUser);
        userMapper.deleteQestion(qidList);
        userMapper.deleteStuQuestion(qidList);
        userMapper.deleteParQuestion(qidList);
        userMapper.deleteTeaQuestion(qidList);
        return null;
    }

    public Object analysisStudent(User user) {
        List<JSONObject> analysisStudents = userMapper.getAnalysisStudent(user);
        return analysisStudents;
    }

    public Object analysisStudentDelete(Analysis_Student analysisStudent) {
        userMapper.deleteAnalysisStudent(analysisStudent);
        return null;
    }

    public Object analysisTeacher(Class c) {
        List<JSONObject> analysisCalssList = userMapper.getAnalysisCalss(c);
        return analysisCalssList;
    }

    public Object analysisTeacherDelete(Analysis_Teacher analysisTeacher) {
        userMapper.deleteAnalysisClass(analysisTeacher);
        return null;
    }

    public Result register(User user) {
        User oldUser = userMapper.getUser(user);
        if(oldUser != null){
            return Result.error("账号已被注册");
        }
        if(userMapper.register(user)){
            return Result.success();
        }
        return Result.error();
    }

    public Result resetPassword(User user) {
        userMapper.resetPassword(user);
        return Result.success();
    }
}
